<?php
/**
 * Created by PhpStorm.
 * User: angl
 * Date: 15/12/24
 * Time: 上午10:50
 */

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Department;
use AuctionCore\Account\Back\Employee;

class DepartController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
    }

    //首页
    public function index()
    {
        $list = Department::getList("id,pid,name");
        // $employees = Employee::get_all();
        $this->assign('list', $list);
        // $this->assign('emps', $employees);
        $this->display();
    }

    //添加部门
    public function addDepart()
    {
        $name = I('post.depart_name');
        if (empty($name)) {
            $this->helpJsonResult(400, "部门名称不能为空");
        }
        $pid = I('post.pid/d');
        if ($pid < 1) {
            $this->helpJsonResult(400, "请选择上级部门");
        }
        $depart = Department::getDetailByName($name, $pid);
        if (!empty($depart)) {
            $this->helpJsonResult(400, "部门名称已存在");
        }
        // $remark = I('post.remark/d','');
        $is_branch_department = I('post.is_branch_department');
        if ($is_branch_department == '1') {
            $data = array(
                'pid' => $pid,
                'name' => $name,
                // 'remark' => $remark,
                'is_branch_department' => $is_branch_department,
            );
            $last_insert_id = Department::create($data);
            $new_Department = new Department($last_insert_id);
            $update = $new_Department->updateDepart(array('branch_id' => $last_insert_id));
        } elseif ($is_branch_department == '0') {
            $data = array(
                'pid' => $pid,
                'name' => $name,
                // 'remark' => $remark,
            );
            $last_insert_id = Department::create($data);
        } else {
            $Department = new Department($pid);
            $depart_info = $Department->detail();
            if ($depart_info['is_branch_department'] == 1) {
                $data = array(
                    'pid' => $pid,
                    'name' => $name,
                    // 'remark' => $remark,
                    'is_branch_department' => 1,
                    'branch_id' => $depart_info['branch_id'],
                );
                $last_insert_id = Department::create($data);
            } else {
                $data = array(
                    'pid' => $pid,
                    'name' => $name,
                    // 'remark' => $remark,
                );
                $last_insert_id = Department::create($data);
            }
        }
        if ($last_insert_id < 1) {
            $this->helpJsonResult(500, "添加部门失败");
        }
        // $emp_id = I('post.leader_id/d',0);
        // if($emp_id>0){
        //     $d = new Department($last_insert_id);
        //     $result = $d->setLeader($last_insert_id, $emp_id);
        //     if(!$result){
        //         //$this->helpJsRedirect("设置负责人失败","window.location.href='/Depart/index'");
        //         $this->helpJsonResult(500, "设置负责人失败");
        //     }
        // }
        //$this->helpJsRedirect("添加成功","window.location.href='/Depart/index'");
        $this->helpJsonResult(200, "添加成功");
    }

    //获取部门信息
    public function getDepart()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult(400, "部门id信息不正确");
        }
        $d = new Department($id);
        $depart_info = $d->detail();
        if (empty($depart_info)) {
            $this->helpJsonResult(404, "部门信息为空");
        }
        //获取对应部门下所有人员
        $leaderhead = $d->getDepartHead();
        $depart_info['leaderhead'] = $leaderhead['emp_id'];
        $depart_info['leaderheadname'] = $leaderhead['real_name'];
        $leaders = $d->getDepartPeople();
        $depart_info['leader'] = $leaders;
        $parent = Department::getDepartById($depart_info['pid']);
        $depart_info['parent_name'] = $parent['name'];
        $this->helpJsonResult(200, 'info', $depart_info);
    }

    //更新部门信息
    public function upDepart()
    {
        $id = I('post.depart_id/d', 0);
        if ($id == 0) {
            $this->helpJsonResult(400, "id信息错误");
        }
        $depart = new Department($id);
        $name = I('post.depart_name', '');
        $pid = I('post.pid/d', 0);
        if ($pid < 1) {
            $this->helpJsonResult(400, "请选择上级部门");
        }
        if (empty($name)) {
            $this->helpJsonResult(400, "部门名称不能为空");
        }
        $depart_info = Department::getDetailByName($name, $pid);
        if (!empty($depart_info) && $depart_info['id'] != $id) {
            $this->helpJsonResult(400, "部门名称已存在");
        }
        $is_branch_department = I('post.is_branch_department');
        if ($pid == 1) {
            if ($is_branch_department == 1) {
                $data = array(
                    'pid' => $pid,
                    'name' => $name,
                    'createtime' => date('Y-m-d h:i:s'),
                    'is_branch_department' => $is_branch_department,
                    'branch_id' => $id
                );
            } else {
                $data = array(
                    'pid' => $pid,
                    'name' => $name,
                    'createtime' => date('Y-m-d h:i:s'),
                    'is_branch_department' => $is_branch_department,
                    'branch_id' => '0'
                );
            }
            $children_id = Department::get_all_childen($id);
            unset($children_id[array_search($id, $children_id)]);
            $result = Department::updateAll($id, $data, $children_id);
        } else {
            $Department = new Department($pid);
            $depart_info = $Department->detail();
            $data = array(
                'pid' => $pid,
                'name' => $name,
                'is_branch_department' => $depart_info['is_branch_department'],
                'branch_id' => $depart_info['branch_id'],
                'createtime' => date('Y-m-d h:i:s')
            );
            $children_id = Department::get_all_childen($id);
            unset($children_id[array_search($id, $children_id)]);
            $result = Department::updateAll($id, $data, $children_id);
            // $result = $depart->updateDepart($data);
        }
        if ($result === false) {
            $this->helpJsonResult(500, "修改部门失败");
        }
        $emp_id = I('post.leader_id/d', 0);
        if ($emp_id > 0) {
            $d = new Department($id);
            $result = $d->setLeader($id, $emp_id);
            if (!$result) {
                $this->helpJsonResult(500, "更新失败");
            }
        }
        $this->helpJsonResult(200, "更新成功");
    }

    //删除部门
    public function delDepart()
    {
        $id = I('post.id/d', 0);
        if ($id == 0) {
            $this->helpJsonResult(400, "id信息错误");
        }
        $depart = new Department($id);
        //检查是否有子节点
        $children = $depart->getChildent();
        if (!empty($children)) {
            $this->helpJsonResult(400, '有下属部门,不能删除');
        }
        $member = $depart->getDepartMember();
        if ($member !== false && !empty($member)) {
            $this->helpJsonResult(400, '部门下还有员工,不能删除!');
        }
        //删除节点
        $result = $depart->delete();
        if (!$result) {
            $this->helpJsonResult(500, '删除失败');
        }
        $this->helpJsonResult(200, '删除成功');
    }

    //获取部门成员
    public function getDepartMember()
    {
        $id = I('post.id/d', 0);
        if ($id < 1) {
            $this->helpJsonResult(400, "ID信息错误");
        }
        $d = new Department($id);
        $list = $d->getDepartMember();
        if (empty($list)) {
            $this->helpJsonResult(500, "no more");
        }
        $this->helpJsonResult(200, 'list', $list);
    }
}